package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.system.domain.query.SysDataboradInfoQuery;
import com.jcfk.system.domain.dto.SysDataboradInfoDTO;
import com.jcfk.system.service.DataboradInfoService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.system.domain.excel.SysDataboradInfoExcel;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据看板
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-01-28
 */
@Api(value = "databoradinfo", tags = {"数据看板"})
@RestController
@RequestMapping("databoradinfo")
public class DataboradInfoController {
    @Autowired
    private DataboradInfoService sysDataboradInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestParam", value = "", required = true)
    })
    @ApiOperation(value = "分页列表", notes = "分页列表", httpMethod = "POST")
    @PostMapping("/queryByPage")
    public RetPage<SysDataboradInfoDTO> queryByPage(@RequestBody ReqPageParam<SysDataboradInfoQuery> requestParam) {
        DataPage<SysDataboradInfoDTO> page = new DataPage<SysDataboradInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            SysDataboradInfoDTO.class);
        page = sysDataboradInfoService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  数据看板
     * 根据id 查询详情
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "数据看板 根据id 查询详情", notes = "数据看板 根据id 查询详情", httpMethod = "GET")
    @GetMapping("/get/{id}")
    public RetResult<SysDataboradInfoDTO> get(@PathVariable("id") String id) {
            SysDataboradInfoDTO item = sysDataboradInfoService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理数据看板
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysDataboradInfoDTO", name = "sysDataboradInfo", value = "", required = true)
    })
    @ApiOperation(value = "添加/更新管理数据看板", notes = "添加/更新管理数据看板", httpMethod = "POST")
    @PostMapping("/save")
    @RequestLogHeader(modular = "数据看板管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysDataboradInfoDTO sysDataboradInfo) {
        // 数据校验
        RetResult ret = sysDataboradInfoService.isValid(sysDataboradInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysDataboradInfo.getId())){
            // 新增
            sysDataboradInfoService.insert(sysDataboradInfo);
        } else{
            // 更新
            sysDataboradInfoService.update(sysDataboradInfo);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除数据看板
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysDataboradInfoDTO", name = "sysDataboradInfo", value = "", required = true)
    })
    @ApiOperation(value = "批量删除数据看板", notes = "批量删除数据看板", httpMethod = "POST")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "数据看板管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysDataboradInfoDTO sysDataboradInfo) {
        return RetResult.ok(sysDataboradInfoService.logicDeleteList(sysDataboradInfo));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysDataboradInfoQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("export")
    public void export(@RequestBody ReqPageParam<SysDataboradInfoQuery> requestParam, HttpServletResponse response) throws Exception {
        List<SysDataboradInfoDTO> list = sysDataboradInfoService.getList(requestParam.getData());
        ExcelUtils.exportExcelToTarget(response, null, list, SysDataboradInfoExcel.class);
    }

}
